package com.tianye.salon.service.impl;


import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.tianye.salon.dao.AdminUserMapper;
import com.tianye.salon.dto.AdminUserFormDto;
import com.tianye.salon.model.AdminUser;
import com.tianye.salon.service.AdminUserService;
import com.tianye.salon.util.CheckUtil;



@Service("adminUserService")
public class AdminUserServiceImpl extends BaseServiceImpl<AdminUser, AdminUserMapper>implements AdminUserService{
	
	final static Logger logger = LoggerFactory.getLogger(AdminUserServiceImpl.class);
	
	@Autowired
	private AdminUserMapper adminUserMapper;

	@Override
	public AdminUserMapper getMapper() throws Exception {
		return adminUserMapper;
	}

	public Map<String, Object> getUserList(Map<String, Object> map) throws Exception {
		Map<String, Object> result = new HashMap<String, Object>();
		List<AdminUser> users = null;
		int page = Integer.parseInt((map.get(grid_page) != null ? map.get(grid_page) : defaultPage).toString());
		int size = Integer.parseInt((map.get(grid_rows) != null ? map.get(grid_rows) : defaultRows).toString());
		map.put(curRow, (page - 1) * size);
		map.put(limitSize, size);
		// 查询总数
		Long count = queryCount(map);
		// 计算总页数
		int totalPage = 1;
		if (null != count && count > 0) {
			if (count % size == 0) {
				totalPage = (int) (count / size);
			} else {
				totalPage = (int) ((count / size) + 1);
			}
		}
		users = this.getMapper().getUserList(map);
		
		result.put("curPage", page);
		result.put("total", count);
		result.put("data", users);
		result.put("totalPage", totalPage);
		return result;
	}

	public AdminUserFormDto selectFormByPrimaryKey(Integer id) {
		return adminUserMapper.selectFormByPrimaryKey(id);
	}
	
	public Map<String, Object> batchDelUsers(List<Integer> list) {
		if(CheckUtil.isEmpty(list)){
			return getFailRtn("参数为空");
		}
		try {
			int cnt = getMapper().batchDelAdminUsers(list);
			if(list.size() == cnt){
				return getSussRtn(null);
			}else{
				logger.error("删除后台用户数量错误");
				return getSussRtn(null);
			}
		} catch (Exception e) {
			e.printStackTrace();
			return getFailRtn("删除失败");
		}
	}
	
	public AdminUser selectByUsername(String username) {
		return adminUserMapper.selectByUsername(username);
	}
}
